﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Think.Lenovo.Com.Models.NewsModel;
using Think.Lenovo.Com.DataAccess.NewsInfo;
using Wuqi.Webdiyer;
using MySql.Data.MySqlClient;
using System.Data;

using System.Reflection;

namespace Think.Lenovo.Com
{
    public partial class ViewNewsList : System.Web.UI.Page
    {
        NewsInfo info = new NewsInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //总部咨询
                AspNetPager_FH.RecordCount = info.GetNewsCount();

                BindData();

                //
                AspNetPager1.RecordCount = info.GetNewsCount();
                BindDataFenQu();
            }
        }

        //总部咨询
        public void BindData()
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            string sql = "Select * from tbl_news order by id desc";
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, AspNetPager_FH.PageSize * (AspNetPager_FH.CurrentPageIndex - 1), AspNetPager_FH.PageSize, "tbl_news");

            Repeater1.DataSource = DataSetToIList<NewsModel>(ds, "tbl_news");

            Repeater1.DataBind();

        }

        public void BindDataFenQu()
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            string sql = "Select * from tbl_news order by id desc";
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "tbl_news");

            Repeater2.DataSource = DataSetToIList<NewsModel>(ds, "tbl_news");

            Repeater2.DataBind();
        }


        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
            {
                NewsModel nm = (NewsModel)e.Item.DataItem;

                Label lnkTitle = (Label)e.Item.FindControl("lab_title");
                lnkTitle.Text = nm.Title;

                Label lab_content = (Label)e.Item.FindControl("lab_Content");
                lab_content.Text = nm.Content;

                Label lab_time = (Label)e.Item.FindControl("lab_time");
                lab_time.Text = nm.ReleaseDate.Value.ToShortDateString();

                Label lbl_btime = (Label)e.Item.FindControl("lab_btime");
                lbl_btime.Text = nm.BrowseTime.ToString();

                Label lbl_target = (Label)e.Item.FindControl("lab_target");
                lbl_target.Text = nm.Target.ToString();

                HyperLink hylink = (HyperLink)e.Item.FindControl("HyperLink1");
                hylink.NavigateUrl = "ViewsNewsPage.aspx?id=" + nm.Id;
            }
        }

        protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
            {
                NewsModel nm = (NewsModel)e.Item.DataItem;

                Label lnkTitle = (Label)e.Item.FindControl("lab_title");
                lnkTitle.Text = nm.Title;

                Label lab_content = (Label)e.Item.FindControl("lab_Content");
                lab_content.Text = nm.Content;

                Label lab_time = (Label)e.Item.FindControl("lab_time");
                lab_time.Text = nm.ReleaseDate.Value.ToShortDateString();

                Label lbl_btime = (Label)e.Item.FindControl("lab_btime");
                lbl_btime.Text = nm.BrowseTime.ToString();

                Label lbl_target = (Label)e.Item.FindControl("lab_target");
                lbl_target.Text = nm.Target.ToString();

                HyperLink hylink = (HyperLink)e.Item.FindControl("HyperLink1");
                hylink.NavigateUrl = "ViewsNewsPage.aspx?id=" + nm.Id;
            }
        }

        #region dataset to ilist
        public IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName)
        {

            int _TableIndex = 0;

            if (p_DataSet == null || p_DataSet.Tables.Count < 0)

                return null;

            if (string.IsNullOrEmpty(p_TableName))

                return null;

            for (int i = 0; i < p_DataSet.Tables.Count; i++)
            {

                // 获取Table名称在Tables集合中的索引值   

                if (p_DataSet.Tables[i].TableName.Equals(p_TableName))
                {

                    _TableIndex = i;

                    break;

                }

            }
            return DataSetToIList<T>(p_DataSet, _TableIndex);

        }

        public IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
        {

            if (p_DataSet == null || p_DataSet.Tables.Count < 0)

                return null;

            if (p_TableIndex > p_DataSet.Tables.Count - 1)

                return null;

            if (p_TableIndex < 0)
                p_TableIndex = 0;


            DataTable p_Data = p_DataSet.Tables[p_TableIndex];

            // 返回值初始化   

            IList<T> result = new List<T>();
            for (int j = 0; j < p_Data.Rows.Count; j++)
            {

                T _t = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _t.GetType().GetProperties();

                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < p_Data.Columns.Count; i++)
                    {

                        // 属性与字段名称一致的进行赋值   
                        if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
                        {
                            // 数据库NULL值单独处理   
                             if (p_Data.Rows[j][i] != DBNull.Value )

                                pi.SetValue(_t, p_Data.Rows[j][i], null);
                            else

                                pi.SetValue(_t, null, null);
                            break;

                        }

                    }

                }

                result.Add(_t);

            }

            return result;
        }

        #endregion

        protected void AspNetPager_FH_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;

            
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindDataFenQu();
        }
    }
}